library(xtable)

load("final/dataIsolated.RData")

kros <- function(x){
  temp <-	sprintf("%1.2f", as.numeric(x))
  # format(c(0.0, 13.1), digits = 2, nsmall = 2)
  if(as.numeric(temp)==0) temp <- "0.00"
  splits <- strsplit(temp, "\\.")[[1]]
  if(splits[1]=="0")	temp <- paste(".",splits[2], sep="")
  #grep("\\.+", 4.22)
  #format()
  if(splits[1]=="-0") temp <- paste("-.", splits[2], sep="")	
  temp
}

reasonall<-data.frame(x=c("Age","Level of Education","Political Party","Martial Status","None of these"), y=c(
  length(dataIsolated$Q44_1[!is.na(dataIsolated$Q44_1)])/nrow(dataIsolated),
  length(dataIsolated$Q44_2[!is.na(dataIsolated$Q44_2)])/nrow(dataIsolated),
  length(dataIsolated$Q44_3[!is.na(dataIsolated$Q44_3)])/nrow(dataIsolated),
  length(dataIsolated$Q44_4[!is.na(dataIsolated$Q44_4)])/nrow(dataIsolated),
  length(dataIsolated$Q44_5[!is.na(dataIsolated$Q44_5)])/nrow(dataIsolated)))

dataIsolated$affpol3<-as.numeric(cut(dataIsolated$affectivepolarization,4))


reason1<- c(length(subset(dataIsolated,affpol3==1)$Q44_1[!is.na(subset(dataIsolated,affpol3==1)$Q44_1)])/nrow(subset(dataIsolated,affpol3==1)),length(subset(dataIsolated,affpol3==1)$Q44_2[!is.na(subset(dataIsolated,affpol3==1)$Q44_2)])/nrow(subset(dataIsolated,affpol3==1)),length(subset(dataIsolated,affpol3==1)$Q44_3[!is.na(subset(dataIsolated,affpol3==1)$Q44_3)])/nrow(subset(dataIsolated,affpol3==1)),length(subset(dataIsolated,affpol3==1)$Q44_4[!is.na(subset(dataIsolated,affpol3==1)$Q44_4)])/nrow(subset(dataIsolated,affpol3==1)),length(subset(dataIsolated,affpol3==1)$Q44_5[!is.na(subset(dataIsolated,affpol3==1)$Q44_5)])/nrow(subset(dataIsolated,affpol3==1)))

reason4<- c(length(subset(dataIsolated,affpol3==4)$Q44_1[!is.na(subset(dataIsolated,affpol3==4)$Q44_1)])/nrow(subset(dataIsolated,affpol3==4)),length(subset(dataIsolated,affpol3==4)$Q44_2[!is.na(subset(dataIsolated,affpol3==4)$Q44_2)])/nrow(subset(dataIsolated,affpol3==4)),length(subset(dataIsolated,affpol3==4)$Q44_3[!is.na(subset(dataIsolated,affpol3==4)$Q44_3)])/nrow(subset(dataIsolated,affpol3==4)),length(subset(dataIsolated,affpol3==4)$Q44_4[!is.na(subset(dataIsolated,affpol3==4)$Q44_4)])/nrow(subset(dataIsolated,affpol3==4)),length(subset(dataIsolated,affpol3==4)$Q44_5[!is.na(subset(dataIsolated,affpol3==4)$Q44_5)])/nrow(subset(dataIsolated,affpol3==4)))

a <- prop.test(
  c(length(subset(dataIsolated,affpol3==1)$Q44_2[!is.na(subset(dataIsolated,affpol3==1)$Q44_2)]),
    length(subset(dataIsolated,affpol3==4)$Q44_2[!is.na(subset(dataIsolated,affpol3==4)$Q44_2)])),
  c(nrow(subset(dataIsolated,affpol3==1)),nrow(subset(dataIsolated,affpol3==4)))
)

b <- prop.test(
  c(length(subset(dataIsolated,affpol3==1)$Q44_3[!is.na(subset(dataIsolated,affpol3==1)$Q44_3)]),
    length(subset(dataIsolated,affpol3==4)$Q44_3[!is.na(subset(dataIsolated,affpol3==4)$Q44_3)])),
  c(nrow(subset(dataIsolated,affpol3==1)),nrow(subset(dataIsolated,affpol3==4)))
)

c <- prop.test(
  c(length(subset(dataIsolated,affpol3==1)$Q44_4[!is.na(subset(dataIsolated,affpol3==1)$Q44_4)]),
    length(subset(dataIsolated,affpol3==4)$Q44_4[!is.na(subset(dataIsolated,affpol3==4)$Q44_4)])),
  c(nrow(subset(dataIsolated,affpol3==1)),nrow(subset(dataIsolated,affpol3==4)))
)

d <- prop.test(
  c(length(subset(dataIsolated,affpol3==1)$Q44_5[!is.na(subset(dataIsolated,affpol3==1)$Q44_5)]),
    length(subset(dataIsolated,affpol3==4)$Q44_5[!is.na(subset(dataIsolated,affpol3==4)$Q44_5)])),
  c(nrow(subset(dataIsolated,affpol3==1)),nrow(subset(dataIsolated,affpol3==4)))
)

table2 <- data.frame(reasonall[1:4,],reason1[1:4],reason4[1:4],paste0("p= ",sapply(c(a$p.value,b$p.value,c$p.value,d$p.value),FUN = kros)))
table2[,2:4] <- mapply(FUN=scales::percent,table2[,2:4])
colnames(table2) <- c("Reason","Overall","Q1 of Affective Polarization","Q4 of Affective Polarization","Significance (Q1-Q4)")
print(xtable(table2,caption="Respondent-Identified Trait(s) Responsible for Exclusion",label="t:reason", align= "lp{1in}P{.5in}P{1in}P{1in}P{1in}"), include.rownames = F, sanitize.colnames.function = identity,caption.placement="top",file="final/avoidreasons.tex")

